<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\store\model;

use app\common\model\RecommendContent as RecommendModel;
use app\store\model\RecommendImage as RecommendImageModel;
use app\store\model\RecommendGoods as RecommendGoodsModel;
use app\common\library\helper;
/**
 * 文章模型
 * Class Article
 * @package app\store\model
 */
class RecommendContent extends RecommendModel
{
  
     /**
     * 获取列表
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [])
    {
        // 查询参数
        $params = $this->setQueryDefaultValue($param, [
            'title' => '',    // 文章标题
            'lock_status' => -1,    // 文章状态
        ]);
        // 检索查询条件
        $filter = [];
        // 文章标题
        !empty($params['title']) && $filter[] = ['title', 'like', "%{$params['title']}%"];
        // 文章状态
        $params['lock_status'] > -1 && $filter[] = ['lock_status', '=', $params['lock_status']];
    
        // 查询列表数据
        $list = $this->with(['image'])
            // ->withoutField(['describe'])
            ->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15);

            // var_dump($list);

            // echo $this->getLastSql();
            return $this->setListData($list);
        // return $list;
    }

    /**
     * 新增记录
     * @param array $data
     * @return false|int
     */
    public function add(array $data)
    {
        if (empty($data['title'])) {
            $this->error = '请输入标题';
            return false;
        }
        if (empty($data['image_id'])) {
            $this->error = '请上传封面图';
            return false;
        }
        if (empty($data['imagesIds'])) {
            $this->error = '请至少上传1个轮播图';
            return false;
        }
        if (empty($data['goods_ids'])) {
            $this->error = '请至少选择1个推荐商品';
            return false;
        }
        if (empty($data['describe'])) {
            $this->error = '请输入内容';
            return false;
        }
        $data['store_id'] = self::$storeId;

        return $this->createData($data);
    }


     /**
     * 添加推荐商品详情相关商品
     * @param array $data
     * @return bool
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function createData(array $data): bool
    {
        // 事务处理
        $this->transaction(function () use ($data) {
            // 添加商品推荐内容
            $this->save($data);
            // 新增商品内容与商品关联
            RecommendGoodsModel::increased((int)$this['recommend_id'], $data['goods_ids']);
            // 新增商品推荐内容与轮播图片关联
            RecommendImageModel::increased((int)$this['recommend_id'], $data['imagesIds']);

        });
        return true;
    }


    /**
     * 更新记录
     * @param array $data
     * @return bool|int
     */
    public function edit(array $data)
    {
        if (empty($data['title'])) {
            $this->error = '请输入标题';
            return false;
        }
        if (empty($data['image_id'])) {
            $this->error = '请上传封面图';
            return false;
        }
        if (empty($data['imagesIds'])) {
            $this->error = '请至少上传1个轮播图';
            return false;
        }
        if (empty($data['goods_ids'])) {
            $this->error = '请至少选择1个推荐商品';
            return false;
        }
        if (empty($data['describe'])) {
            $this->error = '请输入内容';
            return false;
        }
        return $this->updateData1($data);
    }

    /**
     * 添加推荐商品详情相关商品
     * @param array $data
     * @return bool
     * @throws \cores\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateData1(array $data): bool 
    {
        // 事务处理
        $this->transaction(function () use ($data) {
            // 添加商品推荐内容
            $this->save($data);
            // 新增商品内容与商品关联
            RecommendGoodsModel::updates((int)$this['recommend_id'], $data['goods_ids']);
            // 新增商品推荐内容与轮播图片关联
            RecommendImageModel::updates((int)$this['recommend_id'], $data['imagesIds']);

        });
        return true;
    }
    

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 获取文章总数量
     * @param array $where
     * @return int|string
     */
    public static function getArticleTotal(array $where = [])
    {
        return (new static)->where($where)->where('is_delete', '=', 0)->count();
    }



  /**
     * 设置商品推荐详情轮播图或商品展示的数据
     * @param Collection|Paginator $list 商品列表
     * @param callable|null $callback 回调函数
     * @return mixed
     */
    protected static function setListData($list, callable $callback = null)
    {
        if ($list->isEmpty()) return $list;
         $RecommendImageModel = new RecommendImageModel();
        // 遍历商品列表整理数据
        foreach ($list as $k=>$goods) {
            $list[$k]['carousel_images'] =  $RecommendImageModel->getDataList((int)$goods['recommend_id']);
        }
        return $list;
    }

}
